package cn.six1943.deepchat.module.ai.controller.admin.chatsession;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.six1943.deepchat.module.ai.controller.admin.chatsession.vo.ChatMsgRespVO;
import cn.six1943.deepchat.module.ai.controller.admin.chatsession.vo.ChatMsgSaveReqVO;
import cn.six1943.deepchat.module.ai.dal.dataobject.chatsession.ChatMsgDO;
import cn.six1943.deepchat.module.ai.service.chatsession.ChatMsgService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 对话")
@RestController
@RequestMapping("/ai/chat")
@Validated
public class ChatController {

    @Resource
    private ChatMsgService chatMsgService;

    @PostMapping("/create")
    @Operation(summary = "创建会话消息")
    @PreAuthorize("@ss.hasPermission('ai:chat-msg:create')")
    public CommonResult<Long> createChatMsg(@Valid @RequestBody ChatMsgSaveReqVO createReqVO) {
        return success(chatMsgService.createChatMsg(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新会话消息")
    @PreAuthorize("@ss.hasPermission('ai:chat-msg:update')")
    public CommonResult<Boolean> updateChatMsg(@Valid @RequestBody ChatMsgSaveReqVO updateReqVO) {
        chatMsgService.updateChatMsg(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除会话消息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ai:chat-msg:delete')")
    public CommonResult<Boolean> deleteChatMsg(@RequestParam("id") Long id) {
        chatMsgService.deleteChatMsg(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除会话消息")
                @PreAuthorize("@ss.hasPermission('ai:chat-msg:delete')")
    public CommonResult<Boolean> deleteChatMsgList(@RequestParam("ids") List<Long> ids) {
        chatMsgService.deleteChatMsgListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得会话消息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ai:chat-msg:query')")
    public CommonResult<ChatMsgRespVO> getChatMsg(@RequestParam("id") Long id) {
        ChatMsgDO chatMsg = chatMsgService.getChatMsg(id);
        return success(BeanUtils.toBean(chatMsg, ChatMsgRespVO.class));
    }

}